package org.viettan.android.main.activities.articles;

import java.util.List;

import org.viettan.android.main.activities.common.RefreshableListActivity;
import org.viettan.android.main.intents.IntentKeys;
import org.viettan.android.main.io.DownloadEntityTask;
import org.viettan.android.main.io.EntityRetrievalException;
import org.viettan.android.main.managers.DialogManager;
import org.viettan.android.main.managers.SectionManager;
import org.viettan.android.main.managers.SettingsManager;
import org.viettan.android.main.model.Language;
import org.viettan.android.main.model.Section;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.inject.Inject;

/**
 * Displays the short list of sections/columns where articles are categorized under and allows the
 * user to drill into a section.
 */
public class SectionListActivity extends RefreshableListActivity {
    @Inject
    SectionManager sectionManager;
    @Inject
    private DialogManager notificationManager;
    @Inject
    private SettingsManager settingsManager;

    private Language lastLanguage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	loadList(true);
    }

    @Override
    protected void loadList(boolean useCache) {
	lastLanguage = settingsManager.getArticleLanguage();
	new DownloadSectionListTask(this, notificationManager, useCache).execute();
    }

    @Override
    protected void onRestart() {
	super.onRestart();
	if (settingsManager.hasLanguageChanged(lastLanguage)) {
	    loadList(true);
	}
    }

    private void displaySections(List<Section> sections) {
	if (sections == null) {
	    return;
	}
	ArrayAdapter<Section> adapter = new ArrayAdapter<Section>(this,
		android.R.layout.simple_list_item_1, sections);
	setListAdapter(adapter);
    }

    @Override
    protected void onListItemClick(ListView listView, View view, int position, long id) {
	Section section = (Section) getListAdapter().getItem(position);
	Intent sectionIntent = new Intent(this, SectionActivity.class);
	sectionIntent.putExtra(IntentKeys.SECTION_ID.name(), section.getId());
	startActivity(sectionIntent);
    }

    private class DownloadSectionListTask extends
	    DownloadEntityTask<SectionListActivity, List<Section>> {

	private final boolean useCache;

	private DownloadSectionListTask(SectionListActivity SectionListActivity,
		DialogManager notificationManager, boolean useCache) {
	    super(SectionListActivity, notificationManager);
	    this.useCache = useCache;
	}

	@Override
	protected List<Section> getEntity() throws EntityRetrievalException {
	    SectionListActivity activity = getActivity();
	    if (activity == null) {
		return null;
	    }
	    return sectionManager.getSections(useCache);
	}

	@Override
	protected void displayEntity(List<Section> sections) {
	    SectionListActivity activity = getActivity();
	    if (activity == null) {
		return;
	    }
	    activity.displaySections(sections);
	}
    }
}
